package com.johnny.xia.util.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.johnny.xia.util.base.ValidatorUtil;

public class JanrainTest{

    public static void main(String[] args) throws Exception{
        
        Map<String, String> params = new HashMap<String,String>();
        
        String _capture_transactionId=UUID.randomUUID().toString().replaceAll("-", "");
                        
        params.put("capture_screen","otp_registration");
        params.put("capture_transactionId",_capture_transactionId);
        params.put("catalogLocaleItem","zh_CN_CONSUMER");
        params.put("client_id","rxsfurkfnhs885c8dm6zj669hruwnwym");
        params.put("country","CN");
        params.put("createAccountButton","create account");
        params.put("flow","signInGlobal");
        params.put("flow_version","20180709231758740017");
        params.put("form","registrationFormOTP");
        params.put("informationOptIn","true");
        params.put("js_version","d445bf4");
        params.put("locale","zh-CN");
        params.put("mobileNumberRegistration","mobile");
        params.put("preferredLanguage","zh");
        params.put("recaptchaVersion","2");
        params.put("redirect_uri","redirect_uri");
        params.put("response_type","token");
        params.put("settings_version","");
        params.put("utf8","✓");
        
        
        HttpReturnCommand retCommand = post("https://philips-cn.capture.cn.janrain.com/widget/traditional_register.jsonp", params);
        
        System.out.println(retCommand.toString());
        
    }
    
    
    public static HttpReturnCommand post(String url,Map<String,String> params) throws KeyStoreException, KeyManagementException, NoSuchAlgorithmException {
        
        HttpReturnCommand result = new HttpReturnCommand();
        
//        SSLContext sslContext = SSLContextBuilder.create().useProtocol("TLSv1.2").build();
//        
//        CloseableHttpClient httpclient = HttpClients.custom().setSSLContext(sslContext).build();
        
      CloseableHttpClient httpclient = HttpClients.createDefault();
        
        HttpPost httppost = new HttpPost(url);
        
        
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        
        StringBuffer strBuf = new StringBuffer(url).append("?");
        
        //构建httpClient 请求参数。
        if(ValidatorUtil.isNotNullOrEmpty(params)){
            for(Entry<String, String> entry : params.entrySet()){
                formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                
                strBuf.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        
        System.out.println("post info url : "+strBuf.toString());
        
        
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                result.setStatus(response.getStatusLine().getStatusCode());
                if (entity != null) {
                    result.setContent(EntityUtils.toString(entity, "UTF-8"));
                }
                return result;
                
            } finally {
                response.close();
            }
            
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
